﻿using IWshRuntimeLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace wechat_multi_instance
{
    public partial class MainForm : Form
    {
        private Label TimesLb;
        private ComboBox TimesCom;
        private Button StartBtn;
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Font font = new Font("微软雅黑", 14);

            TimesLb = new Label
            {
                Text = "多开次数:",
                Width = 94,
                Height = 33,
                Font = font,
                TextAlign = ContentAlignment.MiddleCenter
            };
            List<string> values = new List<string> { "2", "1" };
            for (int i = 3; i <= 20; i++) values.Add($"{i}");
            TimesCom = new ComboBox
            {
                Width = 66,
                Height = 33,
                Font = font,
                MaxLength = 2,
                DataSource = values,
                DropDownStyle = ComboBoxStyle.DropDownList,
                DropDownHeight = 20 * 8,
            };
            StartBtn = new Button { Text = "一键启动", Width = 100, Height = 33, Font = font };
            OnRight(TimesCom, TimesLb, 0);
            OnRight(StartBtn, TimesCom, 0);
            Controls.Add(TimesLb);
            Controls.Add(TimesCom);
            Controls.Add(StartBtn);
            StartBtn.Click += StartBtnClickEvent;
        }
        private void StartBtnClickEvent(object sender, EventArgs e)
        {
            string weixinLnk = "";
            foreach (string item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory), "*.lnk"))
            {
                if (item.Contains("微信.lnk"))
                {
                    weixinLnk = item;
                    break;
                }
            }
            if (weixinLnk.Equals(""))
            {
                MessageBox.Show("桌面上没有找到【微信.lnk】快捷启动方式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(weixinLnk);
            if (!System.IO.File.Exists(shortcut.TargetPath))
            {
                MessageBox.Show($"【微信.lnk】快捷启动方式指向的程序不存在：\n{shortcut.TargetPath}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (Process.GetProcessesByName("Weixin").Any())
            {
                MessageBox.Show("微信已经在运行！\n请先退出当前的微信程序后再启动多开。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                for (int i = 0; i < int.Parse($"{TimesCom.SelectedItem}"); i++) Process.Start(shortcut.TargetPath);
            }

        }
        private void OnRight(Control c1, Control c2, int space)
        {
            c1.Location = new Point(c2.Location.X + c2.Width + space, c2.Location.Y);
        }
    }
}
